package hlhj.fhp.checkpoint.atys

import android.content.Intent
import com.bumptech.glide.Glide
import com.huantansheng.easyphotos.EasyPhotos
import hlhj.fhp.checkpoint.BaseAty
import hlhj.fhp.checkpoint.R
import hlhj.fhp.checkpoint.customView.MyGlideEngin
import hlhj.fhp.checkpoint.iview.UploadView
import hlhj.fhp.checkpoint.iview.UserInfoView
import hlhj.fhp.checkpoint.javaBean.MyBaseBean
import hlhj.fhp.checkpoint.presenter.UpLoadPresenter
import hlhj.fhp.checkpoint.presenter.UserInfoPresenter
import hlhj.fhp.checkpoint.utils.ToastUtils
import kotlinx.android.synthetic.main.aty_user_info.*
import kotlinx.android.synthetic.main.lo_pub_head.*
import java.io.File

/**
 * Created by Never Fear   on 2018\11\13 0013.
Never More....
 */
class UserInfoAty :BaseAty<UserInfoView,UserInfoPresenter>(), UserInfoView, UploadView {
    private var filePresenter:UpLoadPresenter?=null
    private var f1:File?=null
    private var str:String?=null
    private var head:String?=null
    companion object {

    }
    override fun initView() {
        tvTittle.text="基本资料"
        Glide.with(this).load(intent.getStringExtra("head")).into(btIcon)
        userName.setText(intent.getStringExtra("nick"))
    }
    override fun logicStart() {
    }
    override fun updateResp(myBaseBean: MyBaseBean) {
        ToastUtils.toast("保存成功",this)
        val intent = Intent()
        intent.putExtra("icon",str)
        setResult(-1,intent)
        finish()
    }

    override fun uploadResult(myBaseBean: MyBaseBean, tag: Int) {
        head=myBaseBean.info.toString()
        presenter?.update(userName.text.toString(),head,this)

    }

    private val TAKEPHOTO1: Int=1

    override fun setListener() {
        btIcon.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.checkpoint.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO1)
        }

        btCommit?.setOnClickListener {
            if (f1==null){
                presenter?.update(userName.text.toString(),null,this)
            }else{
                filePresenter?.upload(this, f1!!)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        when(requestCode){
            TAKEPHOTO1->{
                val pathList = data?.getStringArrayListExtra(EasyPhotos.RESULT_PATHS)!!
                Glide.with(this).load(pathList[0]).into(btIcon)
                f1=File(pathList[0])
                str=pathList[0]
            }
        }
    }
    override fun bindView(): UserInfoView {
        return this
    }

    override fun createPresenter(): UserInfoPresenter {
        filePresenter=UpLoadPresenter()
        filePresenter?.let {
            it.createModule()
            it.bindView(this)
        }
        return UserInfoPresenter()
    }


    override val contentId: Int= R.layout.aty_user_info


}